<?php
/*
Template Name: Products
*/
get_header();

//we get the category of this file to pull the information for this page.
$category =  get_the_category();
if($category){
	$category = $category[0];
	$subcategories = get_categories(array('parent' =>  $category->cat_ID, 'hide_empty' => 0, 'orderby' => 'id'));
	?>

	<ul class="f-product-menu row-fluid f-showPhone nav nav-tabs">
		<?php
		$counterMenu = 0; 
		foreach($subcategories as $subcategory) 
		{ 
		$counterMenu++;
		?>
			<li class="<?php if($counterMenu==1){echo 'active';}?>"><a href="#<?php echo $subcategory->name;?>" data-toggle="tab"><?php echo $subcategory->name;?></a></li>
		<?php } ?>
	</ul><!-- cat navbar -->
	<div style="display:none;"><!-- Preload images -->
	   <img src="/wp-content/themes/fructa/images/product-block-bg-hover-last.png" />
	   <img src="/wp-content/themes/fructa/images/product-block-bg-hover-first.png" />
	   <img src="/wp-content/themes/fructa/images/product-block-bg-hover.png" />
	</div>
	<div class="f-product-cat row-fluid tab-content">
		<?php 
			$counter = 0;
			foreach($subcategories as $subcategory) {
				$counter++;
		?>

				<div id="<?php echo $subcategory->name;?>" class="span4 f-product-block tab-pane <?php if($counter==1){echo 'first active';}else if($counter==count($subcategories)){echo 'last';}?>">
					<?php
						$the_query = new WP_Query(array('post_type' => 'attachment', 'post_status' => 'inherit', 'category_name' =>  $subcategory->slug));
						if ($the_query->have_posts()) {
							while ($the_query->have_posts()) {
								$the_query->the_post();					
								echo wp_get_attachment_image($attachment->ID, 'full');					
							}
						}
						wp_reset_postdata();
						?>
						<ul>
							<h2><?php echo $subcategory->name;?></h2>
							<?php 
							$the_query = new WP_Query(array('post_type' => 'pages', 'post_status' => 'publish', 'category_name' =>  $subcategory->slug, 'orderby'=>'menu_order', 'order' => 'ASC'));
							if ($the_query->have_posts()) {
							while ($the_query->have_posts()) {
								$the_query->the_post();					
								?>
								<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
								<?php				
							}
							}
							?>
							<br>
							<i>
							<?php 
							echo category_description($subcategory->cat_ID);
							wp_reset_postdata();
							?>
							</i>
						</ul>
				</div>
		<?php
		} 
		?>
	</div><!-- f-product-cat row-fluid -->

	</div><!-- f-logo-menu -->
	</div><!-- f-header -->
	<?php get_template_part($category->slug, 'grid'); ?>
<?php } //close the cat validation; ?>

<script type="text/javascript">

	$('#bananas a').click(function(e) {
		e.preventDefault();
		$(this).tab('show');
	});
	$('#pineapple a').click(function(e) {
		e.preventDefault();
		$(this).tab('show');
	});
	$('#organics a').click(function(e) {
		e.preventDefault();
		$(this).tab('show');
	})

</script>
<?php
get_footer();
?> 

